#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : importbypdf.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/4/23 16:04                 #
#                                                                   #
#                   @Last Update  : 2021/4/23 16:04                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:导出三个国家待标引的文件                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
# 这是加工服务器的mysql数据库连接属性
import os
import shutil

import pymysql


def copy_pdf(pdf_path:str):
    SYS_PATH = os.path.join("D:\GlobalLawFiles")
    pdf_traget_path = os.path.join("D:", pdf_path)
    pdf_traget_dir = os.path.dirname(pdf_traget_path)
    if not os.path.exists(pdf_traget_dir):
        os.makedirs(pdf_traget_dir)
    print(pdf_path[5:])
    pdf_original_path = os.path.join(SYS_PATH, pdf_path[5:])
    try:
        shutil.copyfile(pdf_original_path,pdf_traget_path)
    except Exception as e:
        with open("copyerro.txt", "a", encoding='utf-8') as f:
            f.write(pdf_original_path + '\n')
        print("文件复制出错:%s(%s)" % (pdf_original_path,str(e)))

# pdf_path = "/PDF/law/Laos/Providedbyexperts/Laws/fb6a51bc58bb7986ec885add2e359c565.pdf"
# copy_pdf(pdf_path)
JGMYSQL_CONNEXT = {
    'MYSQL_HOST': '114.116.40.73',
    'MYSQL_PORT': 3306,
    'MYSQL_DB': 'southwestpoliticsun',
    'MYSQL_USER': 'root',
    'MYSQL_PASSWD': 'xinanzhengfadaqxueapp02!',
    'MYSQL_CHARSET': 'utf8'
}
jgconn = pymysql.connect(host=JGMYSQL_CONNEXT["MYSQL_HOST"],
                              port=JGMYSQL_CONNEXT["MYSQL_PORT"],
                              db=JGMYSQL_CONNEXT["MYSQL_DB"],
                              user=JGMYSQL_CONNEXT["MYSQL_USER"],
                              passwd=JGMYSQL_CONNEXT["MYSQL_PASSWD"],
                              charset=JGMYSQL_CONNEXT["MYSQL_CHARSET"])
jgcuror = jgconn.cursor()
tableS = ["`law`", "`case`"]
for table in tableS:
    sql = '''SELECT FileUrl FROM %s where SYS_FLD_MARKSTATE = "0" AND (sortA = "LAWCOUNTRYXJP" OR SortA = "LAWCOUNTRYFLB" OR SortA = "LAWCOUNTRYWL")''' % table
    try:
        jgcuror.execute(sql)
        trs = jgcuror.fetchall()
    except Exception as e:
        print("数据库查询出错:%s(%s)" % (str(e), sql))
        continue
    for tr in trs:
        pdf_traget_path = tr[0]
        copy_pdf(pdf_traget_path)
jgcuror.close()
jgconn.close()

